﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmHanThanhToan : Form
    {
        private HanThanhToanService httService = new HanThanhToanService();

        public void LoadHanThanhToan(DataTable dbtab)
        {

            lv_htt.Items.Clear();
            //DataTable dbtab = nvService.LayTatCaNV();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                lv_htt.Items.Add(liv);
            }
            
        }

        public void LoadHinhThucThanhToan(DataTable dbtab)
        {
            lv_httt.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                lv_httt.Items.Add(liv);
            }
        }

        public frmHanThanhToan()
        {
            InitializeComponent();
        }

        private void frmHanThanhToan_Load(object sender, EventArgs e)
        {
            DataTable dbtab = httService.LoadHanThanhToan();
            DataTable dbtab1 = httService.LoadHinhThucThanhToan();
            LoadHanThanhToan(dbtab);
            LoadHinhThucThanhToan(dbtab1);
        }

        private void lv_htt_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_htt.FocusedItem.Index >= 0)
            {
                int index = lv_htt.FocusedItem.Index;
                int ma = int.Parse(lv_htt.Items[index].Text.ToString());
                txt_htt.Text = httService.LayHanThanhToanTheoMa(ma);
            }
        }

        private void lv_httt_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_httt.FocusedItem.Index >= 0)
            {
                int index = lv_httt.FocusedItem.Index;
                int ma = int.Parse(lv_httt.Items[index].Text.ToString());
                txt_httt.Text = httService.LayHinhThucThanhToanTheoMa(ma);
            }
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            string hantt = txt_htt.Text;
            if(httService.ThemHanThanhToan(hantt))
            {
                DataTable dbtab = httService.LoadHanThanhToan();
                LoadHanThanhToan(dbtab);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string hanttt = txt_httt.Text;
            if (httService.ThemHinhThucThanhToan(hanttt))
            {
                DataTable dbtab = httService.LoadHinhThucThanhToan();
                LoadHinhThucThanhToan(dbtab);
            }
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            if (lv_htt.FocusedItem.Index >= 0)
            {
                int index = lv_htt.FocusedItem.Index;
                int ma = int.Parse(lv_htt.Items[index].Text.ToString());
                if (httService.XoaHanThanhToan(ma))
                {
                    DataTable dbtab = httService.LoadHanThanhToan();
                    LoadHanThanhToan(dbtab);
                    MessageBox.Show("Xóa thành công");
                    txt_htt.Clear();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (lv_httt.FocusedItem.Index >= 0)
            {
                int index = lv_httt.FocusedItem.Index;
                int ma = int.Parse(lv_httt.Items[index].Text.ToString());
                if (httService.XoaHinhThucThanhToan(ma))
                {
                    DataTable dbtab = httService.LoadHinhThucThanhToan();
                    LoadHinhThucThanhToan(dbtab);
                    MessageBox.Show("Xóa thành công");
                    txt_httt.Clear();
                }
            }
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            if (lv_htt.FocusedItem.Index >= 0)
            {
                int index = lv_htt.FocusedItem.Index;
                int ma = int.Parse(lv_htt.Items[index].Text.ToString());
                if (httService.SuaHanThanhToan(ma,txt_htt.Text))
                {
                    DataTable dbtab = httService.LoadHanThanhToan();
                    LoadHanThanhToan(dbtab);
                    MessageBox.Show("Sửa thành công");
                    txt_htt.Clear();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (lv_httt.FocusedItem.Index >= 0)
            {
                int index = lv_httt.FocusedItem.Index;
                int ma = int.Parse(lv_httt.Items[index].Text.ToString());
                if (httService.SuaHinhThucThanhToan(ma, txt_httt.Text))
                {
                    DataTable dbtab = httService.LoadHinhThucThanhToan();
                    LoadHinhThucThanhToan(dbtab);
                    MessageBox.Show("Sửa thành công");
                    txt_httt.Clear();
                }
            }
        }

        private void btn_huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
